package com.example.basicsecurity.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import static com.example.basicsecurity.constant.Constant.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 罗俊华
 * @date 2021/10/9 - 11:21 上午
 */
@Slf4j
@Controller
public class HelloController {

    AtomicInteger atomicInteger = new AtomicInteger();

    @PreAuthorize("@auth.hasAuth('role_admin')")
    @GetMapping({HELLO,"/"})
    public ModelAndView hello(){

        ModelAndView modelAndView = new ModelAndView("hello");
        modelAndView.addObject("logoutUrl",LOGOUT_URL);
        return modelAndView;
    }

    /**
     * spring security el 表达式中 根对象是
     * org.springframework.security.access.expression.SecurityExpressionRoot
     * 该对象中的方法都可以直接使用
     * @ref https://docs.spring.io/spring-security/reference/servlet/authorization/expression-based.html
     * @return
     */
    @PreAuthorize("hasAnyAuthority('coding')")
    @GetMapping(PATH_1)
    @ResponseBody
    public String coding(){
        return "有写代码的权利："+atomicInteger.getAndIncrement();
    }

    @GetMapping(PATH_1+PATH_2)
    @ResponseBody
    public String m2(){
        return "我是m2方法："+atomicInteger.getAndIncrement();
    }

}
